package com.gxuwz.restaurant.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.restaurant.common.Result;
import com.gxuwz.restaurant.entity.Goods;
import com.gxuwz.restaurant.entity.Role;
import com.gxuwz.restaurant.mapper.GoodsMapper;
import com.gxuwz.restaurant.mapper.MenuMapper;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    GoodsMapper goodsMapper;


    @GetMapping("/select")   //get用来查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") String type){
        System.out.println("执行显示菜品信息");
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getCategory,"1");
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getGoodsname,search);

        }else if (StrUtil.isNotBlank(type)){
            wrapper.like(Goods::getType,type);
        }

        Page<Goods> goodsPage =  goodsMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);


        return Result.success(goodsPage );
    }


    @PostMapping ("/add")//post用来新增
    public Result<?> save(@RequestBody Goods goods){
        System.out.println("执行添加权限管理");
       goods.setCategory("1");//设置为菜品类
        goodsMapper.insert(goods);

        return Result.success();
    }

    @PutMapping ("update")//put用来更新
    public Result<?> update (@RequestBody Goods goods){
        System.out.println("执行更新");

        goodsMapper.updateById(goods);

        return Result.success();


    }

    @DeleteMapping("/{id}") //DELETE用来删除
    public Result<?> delete(@PathVariable long id){
        System.out.println("执行删除方法");
        goodsMapper.deleteById(id);

        return Result.success();


    }

}
